//
//  TXPSelectionPageViewController.m
//  StayAwhile
//
//  Created by dllo on 16/1/27.
//  Copyright © 2016年 dllo. All rights reserved.
//
#import "SPCellFactory.h"
#import "TXPSelectionPageViewController.h"
#import "TXPSPBaseModel.h"
#import "TXPSPWorld.h"
#import "TXPSPBaseTableViewCell.h"
@interface TXPSelectionPageViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *myTableView;
@property(nonatomic,strong)NSMutableArray *allArray;

@end

@implementation TXPSelectionPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.allArray =[NSMutableArray array];
    self.view.backgroundColor =[UIColor grayColor];
    self.myTableView =[[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    [self DATAsyncPostAFNBlock];
    
}

-(void)DATAsyncPostAFNBlock{
    
    [TXPAFNetworkingBlock PostAFNURLWithString:@"http://api2.pianke.me/pub/today" Body:@{@"auth":@"", @"client":@"1", @"deviceid":@"5F17F991-0D41-4B6E-AAC0-4DC946A29CAD", @"limit":@"10",@"start":@"0", @"version":@"3.0.6"} Cookie:@"PHPSESSID=jofk86uf06po8u6eokqon4lak7" resultBlock:^(id result) {
        NSMutableDictionary *dataDic = [result objectForKey:@"data"];
        NSMutableArray *listArray = [dataDic objectForKey:@"list"];
        for (NSMutableDictionary *dic in listArray) {
            NSString *str = [dic objectForKey:@"enname"];
            
            if (!([str isEqualToString:@"Material"]||[str isEqualToString:@"Topic"])) {
                if ([str isEqualToString:@"Sound"]||[str isEqualToString:@"Photo"]||[str isEqualToString:@"Timeline"]||[str isEqualToString:@"Music"]) {
               NSString *className = [@"TXPSP" stringByAppendingString:str];
                Class cellClass = NSClassFromString(className);
                TXPSPBaseModel *model =[[cellClass alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.allArray addObject:model];
                }
                else{
                TXPSPWorld *model =[[TXPSPWorld alloc] init];
                    [model setValuesForKeysWithDictionary:dic];
                    [self.allArray addObject:model];
                }
        }
        }
        [self.myTableView reloadData];
        
    } errorBlock:^(NSError *error) {
        
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.allArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TXPSPBaseModel *model =_allArray[indexPath.row];
    TXPSPBaseTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:NSStringFromClass([model class])];
    if (nil ==cell) {
        cell = [SPCellFactory cellForSPModel:model];
    }
    cell.baseModel = model;
    return cell;
}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 280;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
